package handler

import (
	"net/http"
	"strconv"
	"vod-service/config"

	// 替换为实际项目路径

	"github.com/gin-gonic/gin"
	"github.com/volcengine/volc-sdk-golang/service/vod/models/request"
)

// 1. 创建空间
func CreateSpace(c *gin.Context) {
	var req request.VodCreateSpaceRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, config.Response{
			Code:    400,
			Message: "参数错误: " + err.Error(),
		})
		return
	}

	instance := config.GetVodInstance()
	resp, status, err := instance.CreateSpace(&req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, config.Response{
			Code:    500,
			Message: "创建空间失败: " + err.Error(),
			Data:    gin.H{"status": status},
		})
		return
	}

	c.JSON(http.StatusOK, config.Response{
		Code:    0,
		Message: "success",
		Data:    gin.H{"response": resp, "status": status},
	})
}

// 2. 列举空间
// 修改后的 ListSpace 函数
func ListSpace(c *gin.Context) {
	// 从查询参数获取分页信息
	offsetStr := c.Query("offset")
	limitStr := c.Query("limit")

	req := &request.VodListSpaceRequest{}

	var err error
	if offsetStr != "" {
		req.Offset, err = strconv.ParseFloat(offsetStr, 64)
		if err != nil {
			c.JSON(http.StatusBadRequest, config.Response{
				Code:    400,
				Message: "offset 参数必须是数字",
			})
			return
		}
	}

	if limitStr != "" {
		req.Limit, err = strconv.ParseFloat(limitStr, 64)
		if err != nil {
			c.JSON(http.StatusBadRequest, config.Response{
				Code:    400,
				Message: "limit 参数必须是数字",
			})
			return
		}
	}

	instance := config.GetVodInstance()
	resp, status, err := instance.ListSpace(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, config.Response{
			Code:    500,
			Message: "查询空间列表失败: " + err.Error(),
			Data:    gin.H{"status": status},
		})
		return
	}

	c.JSON(http.StatusOK, config.Response{
		Code:    0,
		Message: "success",
		Data:    gin.H{"response": resp, "status": status},
	})
}

// 3. 获取空间详情
func GetSpaceDetail(c *gin.Context) {
	spaceName := c.Param("spaceName")
	if spaceName == "" {
		c.JSON(http.StatusBadRequest, config.Response{
			Code:    400,
			Message: "空间名称不能为空",
		})
		return
	}

	req := &request.VodGetSpaceDetailRequest{
		SpaceName: spaceName,
	}

	instance := config.GetVodInstance()
	resp, status, err := instance.GetSpaceDetail(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, config.Response{
			Code:    500,
			Message: "获取空间详情失败: " + err.Error(),
			Data:    gin.H{"status": status},
		})
		return
	}

	c.JSON(http.StatusOK, config.Response{
		Code:    0,
		Message: "success",
		Data:    gin.H{"response": resp, "status": status},
	})
}

// 4. 更新空间信息
func UpdateSpace(c *gin.Context) {
	var req request.VodUpdateSpaceRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, config.Response{
			Code:    400,
			Message: "参数错误: " + err.Error(),
		})
		return
	}

	if req.SpaceName == "" {
		c.JSON(http.StatusBadRequest, config.Response{
			Code:    400,
			Message: "空间名称不能为空",
		})
		return
	}

	instance := config.GetVodInstance()
	resp, status, err := instance.UpdateSpace(&req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, config.Response{
			Code:    500,
			Message: "更新空间失败: " + err.Error(),
			Data:    gin.H{"status": status},
		})
		return
	}

	c.JSON(http.StatusOK, config.Response{
		Code:    0,
		Message: "success",
		Data:    gin.H{"response": resp, "status": status},
	})
}

// 5. 更新空间上传配置
func UpdateUploadSpaceConfig(c *gin.Context) {
	var req request.VodUpdateUploadSpaceConfigRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, config.Response{
			Code:    400,
			Message: "参数错误: " + err.Error(),
		})
		return
	}

	if req.SpaceName == "" {
		c.JSON(http.StatusBadRequest, config.Response{
			Code:    400,
			Message: "空间名称不能为空",
		})
		return
	}

	instance := config.GetVodInstance()
	resp, status, err := instance.UpdateUploadSpaceConfig(&req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, config.Response{
			Code:    500,
			Message: "更新上传配置失败: " + err.Error(),
			Data:    gin.H{"status": status},
		})
		return
	}

	c.JSON(http.StatusOK, config.Response{
		Code:    0,
		Message: "success",
		Data:    gin.H{"response": resp, "status": status},
	})
}

// 6. 查询空间上传配置
func DescribeUploadSpaceConfig(c *gin.Context) {
	spaceName := c.Param("spaceName")
	if spaceName == "" {
		c.JSON(http.StatusBadRequest, config.Response{
			Code:    400,
			Message: "空间名称不能为空",
		})
		return
	}

	req := &request.VodDescribeUploadSpaceConfigRequest{
		SpaceName: spaceName,
	}

	instance := config.GetVodInstance()
	resp, status, err := instance.DescribeUploadSpaceConfig(req)
	if err != nil {
		c.JSON(http.StatusInternalServerError, config.Response{
			Code:    500,
			Message: "查询上传配置失败: " + err.Error(),
			Data:    gin.H{"status": status},
		})
		return
	}

	c.JSON(http.StatusOK, config.Response{
		Code:    0,
		Message: "success",
		Data:    gin.H{"response": resp, "status": status},
	})
}

func RegisterSpaceRoutes(r *gin.Engine) {
	// 注册空间管理接口路由
	spaceGroup := r.Group("/api/v1/vod/space")
	{
		spaceGroup.POST("", CreateSpace)                                       // 创建空间
		spaceGroup.GET("", ListSpace)                                          // 列举空间
		spaceGroup.GET("/:spaceName", GetSpaceDetail)                          // 获取空间详情
		spaceGroup.PUT("", UpdateSpace)                                        // 更新空间信息
		spaceGroup.PUT("/upload-config", UpdateUploadSpaceConfig)              // 更新上传配置
		spaceGroup.GET("/upload-config/:spaceName", DescribeUploadSpaceConfig) // 查询上传配置
	}
}
